package util;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import pojo.BackendUser;
import pojo.DevUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Interceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        StringBuffer requestURL = request.getRequestURL();
        if (requestURL.indexOf("/dev/login")!=-1||requestURL.indexOf("/dev/toLogin")!=-1||requestURL.indexOf("/manager/login")!=-1||requestURL.indexOf("/manager/toLogin")!=-1){
            return true;
        }

        BackendUser userSession = (BackendUser) request.getSession().getAttribute("userSession");
        DevUser devUserSession = (DevUser) request.getSession().getAttribute("devUserSession");
        if (userSession!=null || devUserSession!=null){
            return true;
        }
        response.sendRedirect(request.getContextPath()+"/403.jsp");
        return false;
    }
}
